// console.log(__dirname,__filename);
// console.dir(require);
// try{

// }catch(e){
// 	console.dir(e);
// }
var lists = {list:[
		{
			first_name:"Lily"
		}
	],
	title:"Hello Node.js"
}
var patten_var = /(\{.+\})/gm;
var patten_forin = /\{forin name\=[\'|\"](.*)[\'|\"] item\=[\'|\"](.*)[\'|\"]\}\s*(.*)\s*\{\/forin\}/gm;
var patten_if = /\{if condition=[\'|\"]\s*(.*)\s*[\'|\"]}((\s|.)*)\{\/if\}/gm;
function forIn(name, item, body){
	var html = ""
	for (k in lists[name]){
		html+= body.replace(patten_var, function(match){
			var match = match.replace(/\{|\}/g,"");
			var arr = match.split(".");
			var v = lists[name][k];
			for (var i=1; i<arr.length; i++) {
				v = v[arr[i]];
			}
			return v;
		});
	}
	return html;
}

function iF(condition, body){
	console.log(body.replace(/\s*/gm,""));
}

const fs = require('fs');
	fs.readFile("./test.html", 'utf8', function(err,data){
		// console.log(data.match(patten_if));
		// console.log(str);
		let s = data.replace(patten_if, function(match, cond, body){
			iF(cond, body);
			console.log(arguments);
		});
		s = data.replace(patten_forin,function(match, name, item, body){
			// console.log(arguments);
			return forIn(name, item, body);
		});
		s = s.replace(patten_var,function(match){
			// console.log(arguments);
			var match = match.replace(/\{|\}/g,"");
			return lists[match];
		});
		// console.log(s);
	});
// var patten_forin = /(\{forin.*\}([\s|\r|\n].*[\r|\n])(\{\/forin\}))/gm;
// console.log(str.match(patten_forin));
// console.log(str);